<?php
/**
 * Created by IntelliJ IDEA.
 * User: llkj
 * Date: 2018/1/23
 * Time: 17:42
 */

namespace app\controller;


use app\model\FConfigList;
use think\Request;

/**
 * @property FConfigList FConfigList
 */
class Configs extends CommonController
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);

    }
    function index()
    {

        return $this->fetch();
    }
    function ajax_config_save()
    {
        try
        {
            $name = input("name","","trim");
            $value = input("value","","trim");
            if(empty($name))
                $this->E("参数不能为空");
            if(in_array($name,["FREIGHT","CARD_FEE","MAX_PRICE"]))
            {
                $value = floatval($value)*10000;
            }

            if(is_array($value))
            {
                $list = [];
                foreach ($value as $key=>$val)
                {
                    $val = floatval($val);
                    if(!empty($val))
                        $list[] = $val;
                }
                $value = json_encode($list);
            }elseif(in_array($name,["RECHARGE_LOCK_CONTENT"]))
            {
                $value = trim($value);

            }else{
                $value = floatval($value);
            }




            $query = $this->FConfigList->where(["name"=>$name])->update(["value"=>$value]);
            if(empty($query))
            {
                $this->E("更新失败");
            }
            $this->resultdata["error"] = 0;
        }catch (\Exception $e)
        {
            $this->resultdata["msg"] = $e->getMessage();
        }

        echo json_encode($this->resultdata);
    }
}